package com.itcast.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.mapper.EmpExprMapper;
import com.itcast.mapper.EmpMapper;
import com.itcast.pojo.*;
import com.itcast.service.EmpService;
import com.itcast.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    @Autowired
    private EmpExprMapper empExprMapper;

    //原始分页查询
//    @Override
//    public PageResult<Emp> selectByPageAndCount(Integer page, Integer pageSize) {
//        //调用mapper接口 查询分页数据
//        List<Emp> rows = empMapper.findByPage((page-1)*pageSize,pageSize);
//        //调用mapper接口 查询总记录数
//        long total = empMapper.count();
//
//        return new PageResult<Emp>(total,rows);
//    }
    //PageHelper分页查询
//    @Override
//    public PageResult<Emp> selectByPageAndCount(Integer page, Integer pageSize,
//                                                String name, Integer gender, LocalDate begin, LocalDate end) {
//        //设置分页参数
//        PageHelper.startPage(page,pageSize);
//        //执行查询操作
//        List<Emp> rows = empMapper.findByPage(name, gender, begin, end);
//        Page<Emp> page1 = (Page<Emp>) rows;
//        //  封装结果
//        return new PageResult<Emp>(page1.getTotal(),page1.getResult());
//    }

//PageHelper分页查询
    @Override
    public PageResult<Emp> selectByPageAndCount(EmpQueryParam empQueryParam) {
        //设置分页参数
        PageHelper.startPage(empQueryParam.getPage(),empQueryParam.getPageSize());
        //执行查询操作
        List<Emp> rows = empMapper.findByPage(empQueryParam);
        Page<Emp> page1 = (Page<Emp>) rows;
        //  封装结果
        return new PageResult<Emp>(page1.getTotal(),page1.getResult());
    }
    //新增员工
    @Transactional(rollbackFor = Exception.class) //事务注解
    @Override
    public void saveEmp(Emp emp) {
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());
        //保存员工基本信息
        empMapper.saveEmp(emp);
        //保存员工工作经历
        List<EmpExpr> exprList = emp.getExprList();
        if(!CollectionUtils.isEmpty(exprList)){
            exprList.forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.saveEmpExpr(emp.getExprList());
        }

    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteEmpByID(List<Integer> ids) {
        //删除员工基本信息
        empMapper.deleteEmpByID(ids);
        //删除员工经历信息
        empExprMapper.deleteEmpExprByID(ids);

    }
    /*
    *
    * 根据id查询员工信息
    * */
    @Override
    public Emp findEmpById(Integer id) {

        return empMapper.findEmpById(id);
    }
    /**
     *
     * 更新员工信息
     *
     * */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateEmp(Emp emp) {

        //更新员工基本信息
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.updateEmp(emp);
        //删除员工工作经历
        empExprMapper.deleteEmpExprByID(Collections.singletonList(emp.getId()));
        //插入员工经历
        if(!CollectionUtils.isEmpty(emp.getExprList())){
            emp.getExprList().forEach(empExpr -> {
                empExpr.setEmpId(emp.getId());
            });
            empExprMapper.saveEmpExpr(emp.getExprList());
        }

    }
    /*
    * 查询所有员工信息
    * */
    @Override
    public List<Emp> findAllEmp() {
        return empMapper.findAllEmp();
    }

    @Override
    public LoginInfo login(Emp emp) {
        //根据username和password查询
        Emp e =empMapper.selectByUsernameAndPassword(emp);
        if(e != null){
            LoginInfo loginInfo = new LoginInfo();
            loginInfo.setId(e.getId());
            loginInfo.setUsername(e.getUsername());
            loginInfo.setName(e.getName());
            //设置token
            Map<String,Object> claims = new HashMap<>();
            claims.put("id",e.getId());
            claims.put("username",e.getUsername());
            String claimsString =JwtUtils.generateJwt(claims);
            loginInfo.setToken(claimsString);
            return loginInfo;
        }
        return null;
    }

}
